<?php
namespace app\index\model;

use app\common\model\SuperModel;
use app\ux\Json as JsonUx;
use think\Session;

class NoticeBak extends SuperModel
{

    protected $connection = ['prefix' => 'acc_dragon_'];

    public function _initialize()
    {

        parent::_initialize();
        $this->table = 'acc_dragon_notice';
    }
    /**
     * 新建公告
     */
    public function doAdd()
    {
        try {

            $channels = explode(",", input('request.channels'));
            $list     = [];

            //拆分渠道
            //
            foreach ($channels as $channel) {

                $rec['begintime']   = input('request.begintime');
                $rec['endtime']     = input('request.endtime');
                $rec['content']     = input('request.content');
                $rec['title']       = input('request.title');
                $rec['type']        = input('request.type');
                $rec['tag']         = input('request.tag');
                $rec['channel']     = $channel;
                $rec['create_time'] = date("Y-m-d H:i:s");

                if (input('?pictures')) {
                    $rec['pictures'] = input('request.pictures');
                }
                array_push($list, $rec);
            }

            //保存公告
            //
            $res = $this->allowField(true)
                ->saveAll($list);

            //判断结果
            //
            if ($res) {

                $data = getReq();
                $user = Session::get('CURRENT_USER');

                //逐条渠道保存公告日志
                $recs = [];

                foreach ($list as $value) {
                    $data['param']       = JsonUx::json_encode_Unescaped($value);
                    $data['role_name']   = $user['role_name'];
                    $data['staff_name']  = $user['cname'];
                    $data['staff_id']    = $user['user'];
                    $data['create_time'] = date("Y-m-d H:i:s");

                    array_push($recs, $data);
                }

                //$Logs = new Logs;
                //$Logs->saveAll($recs);
                $output = array('success' => true, 'msg' => "发布成功！");

            } else {
                $output = array('success' => false, 'msg' => $this->getError());
            }

            //新增日志
            $this->doLog($output);

            //返回信息
            //
            _callback($output);

        } catch (\Exception $e) {
            $this->doLog($e->getMessage()); //失败日志
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 列表(主键列表，条件数组，闭包查询)
     */
    public function doList($param = null)
    {

        //渠道参数

        if (input('?get.channel')) {
            $channel = input('get.channel');

            if (!empty($channel)) {
                $param['channel'] = $channel;
            }
        }

        //参数查询

        $result = self::all($param);
        $rows   = [];

        foreach ($result as $data) {
            $item         = $data->getData();
            $item['type'] = $data->getAttr('type');
            array_push($rows, $item);
        }

        //查询总计

        $total = count($rows);

        $res = array();

        if ($total > 0) {
            $res['rows']  = $rows;
            $res['total'] = $total;
        } else {
            $res['rows']  = array();
            $res['total'] = 0;
        }

        //返回信息
        //
        _callback($res);

    }
    /**
     * 公告类型获取器
     */
    public function getTypeAttr($value)
    {
        $type = [0 => '文字', 1 => '图像'];
        return $type[$value];
    }
}
